/*
 * Copyright (C) 2021 The Chinese Software International Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.azoft.carousellayoutmanager;

import static com.azoft.carousellayoutmanager.Constant.COLOR_255;
import static com.azoft.carousellayoutmanager.Constant.COLOR_256;

import com.azoft.carousellayoutmanager.adapter.BaseAdapter;

import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;

import java.security.SecureRandom;

/**
 * adapter实现类
 *
 * @since 2021-03-24
 */
public class CustomAdapterImp extends BaseAdapter {
    private final int[] mColors;

    private final int[] mPosition;

    private AbilitySlice abilitySlice;

    /**
     * 构造方法
     *
     * @param abilitySlice AbilitySlice实例
     */
    public CustomAdapterImp(AbilitySlice abilitySlice) {
        this.abilitySlice = abilitySlice;
        int mItemsCount = 100;
        mColors = new int[mItemsCount];
        mPosition = new int[mItemsCount];
        for (int ii = 0; ii < mItemsCount; ii++) {
            SecureRandom random = new SecureRandom();
            mColors[ii] = Color.argb(COLOR_255, random.nextInt(COLOR_256), random.nextInt(COLOR_256),
                    random.nextInt(COLOR_256));
            mPosition[ii] = ii;
        }
    }

    @Override
    public int getCount() {
        return Math.max(mPosition.length, 0);
    }

    @Override
    public Component getComponent(int position, ComponentContainer componentContainer) {
        Component cpt = componentContainer;
        TestViewHolder testViewHolder;
        if (cpt == null) {
            cpt = LayoutScatter.getInstance(abilitySlice).parse(ResourceTable.Layout_item_view,
                    null, false);
            testViewHolder = new TestViewHolder(cpt);
            cpt.setTag(testViewHolder);
        } else {
            testViewHolder = (TestViewHolder) cpt.getTag();
        }
        testViewHolder.textTop.setText(String.valueOf(mPosition[position]));
        testViewHolder.textLeft.setText(String.valueOf(mPosition[position]));
        ShapeElement shapeElement = new ShapeElement();

        shapeElement.setRgbColor(RgbColor.fromArgbInt(mColors[position]));

        testViewHolder.dependentLayout.setBackground(shapeElement);

        testViewHolder.progressWheel.startSpinning();

        return cpt;
    }

    /**
     * 复用的viewHolder
     *
     * @since 2021-03-24
     */
    private static class TestViewHolder {
        private Text textTop;

        private Text textLeft;

        private DependentLayout dependentLayout;

        private ProgressWheel progressWheel;

        TestViewHolder(Component component) {
            textTop = (Text) component.findComponentById(ResourceTable.Id_text_top);
            textLeft = (Text) component.findComponentById(ResourceTable.Id_text_left);
            dependentLayout = (DependentLayout) component.findComponentById(
                    ResourceTable.Id_dependent_background);
            progressWheel = (ProgressWheel) component.findComponentById(ResourceTable.Id_progress_wheel);
        }
    }
}
